package com.yq.spbYq.controller;

import com.yq.spbYq.ViolationRecord;
import com.yq.spbYq.service.impl.ViolationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/violation")
public class ViolationRecordController {

    @Autowired
    private ViolationRecordService violationRecordService;

    // 获取所有违规记录
    @GetMapping("/getAll")
    public ResponseEntity<List<ViolationRecord>> getAllRecords() {
        List<ViolationRecord> records = violationRecordService.getAllRecords();
        return ResponseEntity.ok(records);
    }

    // 根据ID获取违规记录
    @GetMapping("/getById/{vrId}")
    public ResponseEntity<ViolationRecord> getRecordById(@PathVariable Integer vrId) {
        ViolationRecord record = violationRecordService.getRecordById(vrId);
        if (record != null) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 添加违规记录
    @PostMapping("/add")
    public ResponseEntity<ViolationRecord> addRecord(@RequestBody ViolationRecord record) {
        int result = violationRecordService.addRecord(record);
        if (result > 0) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 更新违规记录
    @PutMapping("/update")
    public ResponseEntity<ViolationRecord> updateRecord(@RequestBody ViolationRecord record) {
        int result = violationRecordService.updateRecord(record);
        if (result > 0) {
            return ResponseEntity.ok(record);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 根据ID删除违规记录
    @DeleteMapping("/delete/{vrId}")
    public ResponseEntity<Void> deleteRecordById(@PathVariable Integer vrId) {
        int result = violationRecordService.deleteRecordById(vrId);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}